const {
	Controller
} = require('uni-cloud-router')
const uniID = require('uni-id')
const dbUtil = require('db-util')
module.exports = class CustomerController extends Controller {
	async recharge() {
		let {
			customerId,
			rechargeAmount,
			giveAmount
		} = this.ctx.data
		if (isNaN(parseInt(rechargeAmount)) ||
			isNaN(parseInt(giveAmount)) ||
			parseInt(rechargeAmount) <= 0 ||
			parseInt(giveAmount) < 0 ||
			!customerId) {
			return {
				code: 500,
				msg: '参数错误！'
			}
		}
		let customer = await dbUtil.getById({
			collection: 't3ds-customer',
			id: customerId
		})
		if (!customer) {
			return {
				code: 500,
				msg: '客户不存在！'
			}
		}
		return this.service.customer.recharge({
			customer,
			rechargeAmount: parseInt(rechargeAmount),
			giveAmount: parseInt(giveAmount)
		})
	}
	
	async consume() {
		let {
			customerId,
			consumeAmount
		} = this.ctx.data
		console.log('consume', customerId, consumeAmount)
		if (isNaN(parseInt(consumeAmount)) ||
			parseInt(consumeAmount) <= 0 ||
			!customerId) {
			return {
				code: 500,
				msg: '参数错误！'
			}
		}
		let customer = await dbUtil.getById({
			collection: 't3ds-customer',
			id: customerId
		})
		if (!customer) {
			return {
				code: 500,
				msg: '客户不存在！'
			}
		}
		if (customer.balance < parseInt(consumeAmount)) {
			return {
				code: 500,
				msg: '余额不足！'
			}
		}
		return this.service.customer.consume({
			customer,
			consumeAmount: parseInt(consumeAmount)
		})
	}
}
